package com.liang.springbatch.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.step.builder.JobStepBuilder;
import org.springframework.batch.core.step.builder.StepBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;

/**
 * @ClassName NestedDemo
 * @Description TODO
 * @Author liang
 * @Date 19-6-29 下午6:40
 * @Version 1.0
 **/
@Configuration
public class NestedDemo {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private Job childJobOne;
    @Autowired
    private Job childJobTwo;

    @Autowired
    private JobLauncher jobLauncher;
    @Bean
    public Job parentJob(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager){
        return jobBuilderFactory.get("parentJob")
                .start(childJob1(jobRepository,platformTransactionManager))
                .next(childJob2(jobRepository,platformTransactionManager))
                .build();
    }

    //返回的是job类型的step
    private Step childJob2(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager) {

        return new JobStepBuilder(new StepBuilder("childJob2"))
                .job(childJobTwo)
                .launcher(jobLauncher)
                .repository(jobRepository)
                .transactionManager(platformTransactionManager)
                .build()
                ;
    }

    private Step childJob1(JobRepository jobRepository, PlatformTransactionManager platformTransactionManager) {

        return new JobStepBuilder(new StepBuilder("childJob1"))
                .job(childJobOne)
                .launcher(jobLauncher)
                .repository(jobRepository)
                .transactionManager(platformTransactionManager)
                .build()
                ;
    }
}
